#include "FileMock.h"
#include "../storage/storage.h"
#include <IDirectory.h>
#include <auto_ptr.h>
#include <vector>

using namespace storagemon;
using ::testing::Return;

class DirectoryObjectTest : public testing::Test
{
public:
    void SetUp()
    {
        m_directory.reset(new DirectoryObject("alexey", "/home/alexey", 2, 1));
    }

    void TearDown()
    {
    }

protected:
    std::auto_ptr<IDirectory> m_directory;
    mock::FileObjectMock m_fileMock1;
    mock::FileObjectMock m_fileMock2;
};

TEST_F(DirectoryObjectTest, GetId)
{
    ASSERT_EQ(m_directory.get()->id(), 2);
}

TEST_F(DirectoryObjectTest, GetParentId)
{
    ASSERT_EQ(m_directory.get()->parent_id(), 1);
}

TEST_F(DirectoryObjectTest, Name)
{
    ASSERT_STREQ(m_directory.get()->name(), "alexey");
}

TEST_F(DirectoryObjectTest, Path)
{
    ASSERT_STREQ(m_directory.get()->path(), "/home/alexey");
}

TEST_F(DirectoryObjectTest, AddFile)
{
    ASSERT_TRUE(m_directory.get()->add_file(&m_fileMock1));
    ASSERT_EQ(m_directory.get()->size(), 1);
}

TEST_F(DirectoryObjectTest, RemoveFile)
{
    ASSERT_TRUE(m_directory.get()->add_file(&m_fileMock1));
    EXPECT_CALL(m_fileMock1, id())
        .Times(1)
        .WillOnce(Return(1));
    ASSERT_TRUE(m_directory.get()->remove_file(1));
}

TEST_F(DirectoryObjectTest, AddExistingFile)
{
    EXPECT_CALL(m_fileMock1, id())
        .Times(2)
        .WillRepeatedly(Return(1));

    EXPECT_CALL(m_fileMock1, parent_id())
        .Times(2)
        .WillRepeatedly(Return(1));

    EXPECT_CALL(m_fileMock1, full_path())
        .Times(2)
        .WillRepeatedly(Return("/home/alexey/test.txt"));
    ASSERT_TRUE(m_directory.get()->add_file(&m_fileMock1));
    ASSERT_FALSE(m_directory.get()->add_file(&m_fileMock1));
}

TEST_F(DirectoryObjectTest, AddNotExistingFile)
{
    EXPECT_CALL(m_fileMock1, id())
        .Times(1)
        .WillRepeatedly(Return(1));

    EXPECT_CALL(m_fileMock1, parent_id())
        .Times(1)
        .WillRepeatedly(Return(1));

    EXPECT_CALL(m_fileMock1, full_path())
        .Times(1)
        .WillRepeatedly(Return("/home/alexey/test.txt"));

    EXPECT_CALL(m_fileMock2, id())
        .Times(1)
        .WillRepeatedly(Return(2));

    EXPECT_CALL(m_fileMock2, parent_id())
        .Times(1)
        .WillRepeatedly(Return(1));

    EXPECT_CALL(m_fileMock2, full_path())
        .Times(1)
        .WillRepeatedly(Return("/home/alexey/test2.txt"));
    ASSERT_TRUE(m_directory.get()->add_file(&m_fileMock1));
    ASSERT_TRUE(m_directory.get()->add_file(&m_fileMock2));
}